package club.ljwang.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Auther: wangl
 * @Date: 2020/3/4 00:18
 * @Description:
 */
@Component
public class JedisUtil {

    @Autowired
    private JedisPool jedisPool;

    public String setex(String key, int seconds, String value) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            resource.setex(key, seconds, value);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
        return "";

    }

    public String set(String key, String value) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            resource.set(key, value);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
        return "";

    }


    public String get(String key) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            return resource.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
        return "";

    }

    public Long delete(String key) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            return resource.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
        return 0L;

    }
}
